/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = __frstat_thread_count.c                                */
/*  NOTE      =                                                    */
/*  DATE      = 2016/04/18 by geyh                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frstat_thread.h"

#include "frstat_module.h"


static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

typedef struct __thread_info {
    unsigned long ul_task_dones;
    unsigned long ul_errors;
    unsigned long ul_requests;
    unsigned long ul_responses;
} Frstat_thread_info;

typedef struct __thread_info_avg {
    unsigned long ul_task_dones[4];
    unsigned long ul_errors[4];
    unsigned long ul_requests[4];
    unsigned long ul_responses[4];
} Frstat_thread_avg;

typedef struct __thread_stat {
    struct timeval start_time; //线程开始时间
    struct timeval run_time; //线程开始时间

    unsigned long int i_thread_id;
    Frstat_thread_info st_thread_total;
    Frstat_thread_avg  st_thread_avg;
} Frstat_thread;

int __frstat_thread_count(Frlist_node* p_node, const void *p_data);

void __stat_thread_node_free(Frlist_node* p_node);

static void __case1(void)
{
    int i_ret = 0;
    Frlist_node* p_node = NULL;
    const void *p_data = NULL;
    Frstat_thread *p_stat_thread = NULL;

    p_stat_thread = (Frstat_thread *)frmalloc(sizeof(Frstat_thread));
    memset(p_stat_thread, 0x00, sizeof(Frstat_thread));

    p_stat_thread->st_thread_total.ul_task_dones  = 10;
    p_stat_thread->st_thread_total.ul_errors   = 10;
    p_stat_thread->st_thread_total.ul_requests   = 10;
    p_stat_thread->st_thread_total.ul_responses   = 10;

    p_node = frlist_node_create(p_stat_thread);
    i_ret = __frstat_thread_count(p_node,p_data);

    CU_ASSERT_EQUAL(i_ret,1);
    CU_ASSERT_EQUAL(p_stat_thread->st_thread_total.ul_task_dones,10);
    CU_ASSERT_EQUAL(p_stat_thread->st_thread_total.ul_errors,10);
    CU_ASSERT_EQUAL(p_stat_thread->st_thread_total.ul_requests,10);
    CU_ASSERT_EQUAL(p_stat_thread->st_thread_total.ul_responses,10);
    CU_ASSERT_EQUAL(p_stat_thread->st_thread_avg.ul_task_dones[0],10);
    CU_ASSERT_EQUAL(p_stat_thread->st_thread_avg.ul_errors[0],10);
    CU_ASSERT_EQUAL(p_stat_thread->st_thread_avg.ul_requests[0],10);
    CU_ASSERT_EQUAL(p_stat_thread->st_thread_avg.ul_responses[0],10);

    __stat_thread_node_free(p_node);
    frfree(p_node);

}



int add_suite___frstat_thread_count(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frstat_thread __frstat_thread_count()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
//    CU_add_test(pSuite, "case2", __case2);
//    CU_add_test(pSuite, "case3", __case3);
//    CU_add_test(pSuite, "case4", __case4);

    return FR_SUCC;
}
